package sis.ui;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-04 16:27
 * @notify
 * @version 1.0
 */

import sis.studentinfo.Course;
import sis.util.ImageUtil;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import java.awt.*;
import java.awt.event.*;

public class Sis {
    public static final int WIDTH = 350;
    public static final int HEIGHT = 500;
    //标题
    public static final String COURSES_TITLE = "Course Listing";

    //窗体设置标题
    private JFrame frame = new JFrame(COURSES_TITLE);

    //面板对象
    public CoursesPanel panel;

    public static void main(String[] args) {
        new Sis().show();
    }

    //构造子
    public Sis() {
        initialize();
    }

    private void initialize() {
        //创建面板
        createCoursesPanel();
        //创建点击文本监听事件
        createKeyListeners();
        //创建过滤器
        createInputFilter();
        //设置窗体图标
        Image image = ImageUtil.create("/fans_icon.png");
        frame.setIconImage(image);

        //设置窗体宽高
        frame.setSize(WIDTH, HEIGHT);
        //设置关闭
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //添加一个自定义面板,该面板包含其他容器和组件
        frame.getContentPane().add(panel);
    }

    //创建主面板
    public void createCoursesPanel() {
        panel = new CoursesPanel();
        //给面板中的按钮添加点击事件
        panel.addCourseAddListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addCourse();
            }
        });
    }

    //绑定过滤器
    private void createInputFilter(){
        JTextField field = panel.getField(CoursesPanel.DEPARTMENT_FIELD_NAME);
        AbstractDocument document = (AbstractDocument)field.getDocument();
        document.setDocumentFilter(new UpcaseFilter());
    }

    //绑定文本监听事件
    public void createKeyListeners(){
        KeyListener listener = new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                setAddButtonState();
            }
        };
        panel.addFieldListener(CoursesPanel.DEPARTMENT_FIELD_NAME,listener);
        panel.addFieldListener(CoursesPanel.NUMBER_FIELD_NAME,listener);
        setAddButtonState();
    }

    //设置按钮是否可用,如果两个文本内容有一个是false则设置false
    public void setAddButtonState(){
        panel.setEnabled(CoursesPanel.ADD_BUTTON_NAME,!isEmpty(CoursesPanel.DEPARTMENT_FIELD_NAME)&&
                !isEmpty(CoursesPanel.NUMBER_FIELD_NAME));
    }
    //检验文本内容是否是空
    private boolean isEmpty(String field){
        String value = panel.getText(field);
        return value.trim().equals("");
    }

    //点击事件触发
    private void addCourse() {
        //获取两个输入框中的值,组装成一个 课程对象
        Course course = new Course(panel.getText(CoursesPanel.DEPARTMENT_FIELD_NAME),
                panel.getText(CoursesPanel.NUMBER_FIELD_NAME));
        //将课程对象添加到list中
        panel.addCourse(course);
    }


    //显示窗体
    public void show() {
        frame.setVisible(true);
    }

    //返回窗体
    public JFrame getFrame() {
        return frame;
    }

    //关闭窗体
    public void close() {
        frame.dispose();
    }

}
